package cn.gmcx.lazy.izze.service.impl;

import cn.gmcx.lazy.izze.enums.DynamicTypeEnum;
import cn.gmcx.lazy.izze.exception.BizException;
import cn.gmcx.lazy.izze.mapper.UserMapper;
import cn.gmcx.lazy.izze.pojo.Dynamic;
import cn.gmcx.lazy.izze.mapper.DynamicMapper;
import cn.gmcx.lazy.izze.pojo.DynamicVo;
import cn.gmcx.lazy.izze.pojo.User;
import cn.gmcx.lazy.izze.service.IDynamicService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 项目动态，保存角色操作 服务实现类
 * </p>
 *
 * @author GMCX
 * @since 2024-07-01
 */
@Service
public class DynamicServiceImpl extends ServiceImpl<DynamicMapper, Dynamic> implements IDynamicService {
    @Autowired
    private DynamicMapper dynamicMapper;
    @Autowired
    private UserMapper userMapper;
    @Override
    public void addDynamic(Integer id, DynamicTypeEnum dynamicTypeEnum, String operate) {
        Dynamic dynamic = new Dynamic();
        dynamic.setUserId(id);
        dynamic.setType(dynamicTypeEnum);
        dynamic.setOperate(operate);
        int insert = dynamicMapper.insert(dynamic);
        if(insert == 0) {
            throw new BizException("动态创建失败");
        }
    }

    @Override
    public List<DynamicVo> getDynamicList(Integer id) {
        User user = userMapper.selectById(id);

        List<DynamicVo> dynamicVoList = dynamicMapper.getDynamicList(id);
        dynamicVoList.forEach(item->{
            item.setUsername(user.getUsername());
        });
         return dynamicVoList;
    }
}
